package com.glory.wxelectricitypda.activity;


import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.ItemShelfAdapter;
import com.glory.wxelectricitypda.adapter.ScrapShelvesAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.BACKShelfBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class ScrapOffShelvesActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.rvRelocationInfoList)
    RecyclerView rvRelocationInfoList;

    private ScrapShelvesAdapter mItemShelfAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_scrap_off_shelves;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("不良品下架");

        rvRelocationInfoList.setLayoutManager(new LinearLayoutManager(ScrapOffShelvesActivity.this));
        rvRelocationInfoList.setItemAnimator(new DefaultItemAnimator());
        rvRelocationInfoList.addItemDecoration(new DividerItemDecoration(ScrapOffShelvesActivity.this, 1));
        mItemShelfAdapter = new ScrapShelvesAdapter(null);
        mItemShelfAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mItemShelfAdapter.isFirstOnly(true);
        rvRelocationInfoList.setAdapter(mItemShelfAdapter);
    }

    @Override
    protected void initData() {
        initEdit(etLotID, s -> {
            itemShelfQuery();
        });
    }

    private void itemShelfQuery() {
        JSONObject map = new JSONObject();
        JSONObject lotid = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONTYPE", "itemUnshelf");
            lotid.put("LOTID", etLotID.getText().toString());
            map.put("PARENTLOT", lotid);
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.STORAGE");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<BACKShelfBean>>(
                new TypeToken<ResponseBean<BACKShelfBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<BACKShelfBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                    if (response.body().getBody().getLOT_SCRAPS().size() > 0) {
                        showToast("下架成功", true);
                        mItemShelfAdapter.addData(response.body().getBody().getLOT_SCRAPS().get(0));
                        etLotID.setText("");
                        new Handler().postDelayed(() -> {
                            etLotID.requestFocus();
                        }, 500);
                    }
                }
            }

            @Override
            public void onError(Response<ResponseBean<BACKShelfBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EUIT.TESTBANK");
            }
        });
    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header", llMain);
    }
}